import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import junit.framework.TestCase;


public abstract class BaseIndexingTestCase extends TestCase{
	String[] keywords = {"1", "2"};
	String[] unindexed = {"Netherlands", "Italy"};
	String[] unstored = {"Amsterdam has lots of bridges", "Venice has lots of canals"};
	String[] text = {"Amsterdam", "Venice"};
	Directory dir;
	
	protected void setUp() throws Exception {
		String indexDir = System.getProperty("java.io.tmpdir", "tmp") + System.getProperty("file.separator") + "index-dir";
		dir = FSDirectory.getDirectory(indexDir, true);
		addDocuments(dir);
	}
	
	private void addDocuments(Directory dir) throws IOException {
		IndexWriter writer = new IndexWriter(dir, getAnalyzer(), true);
		writer.setUseCompoundFile(isCompound());
		for (int i = 0; i < keywords.length; i++) {
			Document doc = new Document();
			doc.add(Field.Keyword("id", keywords[i]));
			doc.add(Field.UnIndexed("country", unindexed[i]));
			doc.add(Field.UnStored("contents", unstored[i]));
			doc.add(Field.Text("city", text[i]));
			writer.addDocument(doc);
		}
		writer.optimize();
	}

	private boolean isCompound() {
		return true;
	}

	private Analyzer getAnalyzer() {
		return new SimpleAnalyzer();
	}
	
}
